package com.sunnos.express.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sunnos.express.entity.Right;
import com.sunnos.express.entity.Role;
import com.sunnos.express.entity.User;
import com.sunnos.express.service.RoleService;

@Controller
@RequestMapping("/role")
public class RoleController {
	@Autowired
	private RoleService roleService;
	
	/**
	 * ��ѯ��ɫ�б�
	 * @param request
	 * @param response
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/list")
	public @ResponseBody Map<String,Object> list(HttpServletRequest request,   
			HttpServletResponse response){
		//spring������jackson�Զ�������ֵ��װ��JSON����
		Map<String,Object> responseMap = new HashMap<String,Object>();

		try {
			Map<String,Object> param = new HashMap<String,Object>();
			int totalCount = roleService.getRoleListTotal();
			List<Role> ret = roleService.getRoleList(param);
			responseMap.put("rows", ret);
			responseMap.put("totalCount", totalCount);
			responseMap.put("success", "true");
			return responseMap;
		}catch(Exception e) {
			e.printStackTrace();
			responseMap.put("success", "false");
			responseMap.put("info", e.getClass()+":"+e.getMessage());
			return responseMap;
		}
	}
	
	/**
	 * ��ѯȨ����Ϣ��
	 * @param request
	 * @param response
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/getRights")
	public @ResponseBody Map<String,Object> getRights(HttpServletRequest request,   
			HttpServletResponse response){
		//spring������jackson�Զ�������ֵ��װ��JSON����
		Map<String,Object> responseMap = new HashMap<String,Object>();
		try {
			List<Right> topRights = roleService.getTopRighs();
			List<Hashtable<String,Object>> comps = new ArrayList<Hashtable<String,Object>>();
			//StringBuffer sb = new StringBuffer();
			for(int i = 0 ; i < topRights.size() ; i++){
				Right topright = topRights.get(i);
				Hashtable<String,Object> h = new Hashtable<String,Object>();							
				List<Hashtable<String,Object>> fieldsItemsList = new ArrayList<Hashtable<String,Object>>();
				List<Hashtable<String,Object>> checkboxgroupItemsList = new ArrayList<Hashtable<String,Object>>();
				Hashtable<String,Object> filedsItemsH = new Hashtable<String,Object>();				
				Hashtable<String,Object> hidCheckBox = new Hashtable<String,Object>();
				hidCheckBox.put("boxLabel", topright.getName());
				hidCheckBox.put("id", "ckbxf"+i);
				hidCheckBox.put("hidden", "true");
				hidCheckBox.put("checked", false);
				hidCheckBox.put("name", String.valueOf(topright.getId()));
				checkboxgroupItemsList.add(hidCheckBox);
				List<Right> subRights = roleService.getSubRighs(topright.getId());
				for(int j = 0 ; j < subRights.size() ; j++){
					Right subright = subRights.get(j);
					Hashtable<String,Object> subh = new Hashtable<String,Object>();
					subh.put("boxLabel", subright.getName());
					subh.put("name", String.valueOf(subright.getId()));
					checkboxgroupItemsList.add(subh);
				}
				filedsItemsH.put("items", checkboxgroupItemsList);
				filedsItemsH.put("columns", 3);
				filedsItemsH.put("id", "cxbxg"+i);
				filedsItemsH.put("xtype", "checkboxgroup");				
				fieldsItemsList.add(filedsItemsH);
				h.put("items", fieldsItemsList);
				h.put("anchor","90%");	
				h.put("autoHeight","true");
				h.put("title",topright.getName());
				h.put("xtype","fieldset");
				comps.add(h);
//				sb.append("id:\"cxbxg"+i+"\",xtype:\"checkboxgroup\",columns: 2,items: [");
//				sb.append("{boxLabel : \""+topright.getName()+"\",id:\"ckbxf"+i+"\",hidden:true,checked:false,name :\""+topright.getId()+"\"},");
//				List<Right> subRights = roleService.getSubRighs(topright.getId());
//				for(int j = 0 ; j < subRights.size() ; j++){
//					Right subright = subRights.get(j);
//					sb.append("{boxLabel : \""+subright.getName()+"\",name :\""+subright.getId()+"\"},");
//				}
//				sb.delete(sb.toString().length()-1, sb.toString().length());
//				sb.append("]}]},");
			}
//			sb.delete(sb.toString().length()-1, sb.toString().length());
//			System.out.println("�����Ȩ��ѡ������ַ��ǣ�................"+comps.toString());
//			responseMap.put("data", sb.toString());
			responseMap.put("data", comps);
			responseMap.put("totaltop", topRights.size());
			responseMap.put("success", "true");
			responseMap.put("msg", "ok");
			return responseMap;
		}catch(Exception e) {
			e.printStackTrace();
			responseMap.put("success", "false");
			responseMap.put("info", e.getClass()+":"+e.getMessage());
			return responseMap;
		}
	}
	
	/**
	 * ����ɫ���Ƿ�ע��
	 * @param request
	 * @param response
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/checkAccount",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> checkAccount(HttpServletRequest request,   
			HttpServletResponse response){
		//spring������jackson�Զ�������ֵ��װ��JSON����
		Map<String,Object> responseMap = new HashMap<String,Object>();
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String ret = "failure";
		//System.out.println("check name return paramer is .............."+id+","+name);
		try {
			Map<String,Object> param = new HashMap<String,Object>();
			if (null==id || "".equals(id) || "null".equals(id)) {
				if (null!=name && !"".equals(name)) {
					param.put("id", "");
					param.put("name", name);
					Role role = roleService.getRole(param);			
					if(role!=null) {
						ret = "ok";
					}else {
						ret = "failure";
					}
				}
			}
			else {
				if (null!=name && !"".equals(name)) {
					param.put("name", name);
					param.put("id", "");
					Role role = roleService.getRole(param);			
					if(role!=null) {
						param.put("id", id);
						param.put("name", "");
						Role oraginRole = roleService.getRole(param);
						if (oraginRole!=null) {
							if (oraginRole.getName().equals(role.getName())) {
								ret = "failure";
							}
							else {
								ret = "ok";
							}
						}
					}else {
						ret = "failure";
					}
				}
			}
			responseMap.put("msg", ret);
			responseMap.put("info", "�û��˺Ŵ���");
			return responseMap;
		}catch(Exception e) {
			e.printStackTrace();
			responseMap.put("msg", "failure");
			responseMap.put("info", e.getClass()+":"+e.getMessage());
			return responseMap;
		}
	}
	
	/**
	 * ���ָ����ɫ����
	 * @param request
	 * @param response
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/getRole",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> getRole(HttpServletRequest request,   
			HttpServletResponse response){
		//spring������jackson�Զ�������ֵ��װ��JSON����
		Map<String,Object> responseMap = new HashMap<String,Object>();
		String id = request.getParameter("id");
		try {
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("id", id);
			Role role = roleService.getRole(param);
			responseMap.put("msg", "ok");
			responseMap.put("role", role);
			responseMap.put("info", "��ȡָ����ɫ����ɹ�");
			return responseMap;
		}catch(Exception e) {
			e.printStackTrace();
			responseMap.put("msg", "failure");
			responseMap.put("info", e.getClass()+":"+e.getMessage());
			return responseMap;
		}
	}
	
	/**
	 * ��ɾ���ɫ
	 * @param request
	 * @param response
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public @ResponseBody Map<String,String> delete(HttpServletRequest request,   
			HttpServletResponse response){
		//spring������jackson�Զ�������ֵ��װ��JSON����
		Map<String,String> responseMap = new HashMap<String,String>();
		String ids = request.getParameter("id");	
		try {
			System.out.println("ids is...."+ids);
			Map<String,Object> param = new HashMap<String,Object>();
			String[] idsArray = ids.split(",");
			for(int i=0;i<idsArray.length;i++) {
				param.put("role_id", idsArray[i]);
				List<User> list = roleService.getRoleUserList(param);
				System.out.println("��ѯ��ɫ�µ��û��б?�أ�....."+list);
				if (list.size()>0) {
					responseMap.put("msg", "failure");
					responseMap.put("success", "true");
					responseMap.put("info", "��ý�ɫid��"+idsArray[i]+"���ѷ�����û�ʹ�ã�����ɾ�����ڸý�ɫ���û������ɾ����û�");
					return responseMap;
				}
				else {
					roleService.deleteRole(Integer.parseInt(idsArray[i]));
				}
			}			
			responseMap.put("msg", "ok");
			responseMap.put("success", "true");
			responseMap.put("info", "�ɹ�");
			return responseMap;
		}catch(Exception e) {
			e.printStackTrace();
			responseMap.put("msg", "failure");
			responseMap.put("success", "false");
			responseMap.put("errors", e.getClass()+":"+e.getMessage());
			return responseMap;
		}
	}
	
	/**
	 * ��ӻ��޸��û�
	 * @param request
	 * @param response
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public @ResponseBody Map<String,String> save(HttpServletRequest request,   
			HttpServletResponse response){
		//spring������jackson�Զ�������ֵ��װ��JSON����
		Map<String,String> responseMap = new HashMap<String,String>();
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String rights = request.getParameter("rights");
		System.out.println("save parameter is ....................(id,name,rights)"+id+","+name+","+rights);
		try {
			if (id==null || "".equals(id) || "null".equals(id)) {
				Map<String,Object> param = new HashMap<String,Object>();
				param.put("name", name);
				param.put("rights", rights);
				roleService.insertRole(param);	
				if (!"".equals(rights) && rights != null) {
					Role role = roleService.getRole(param);
					if (role != null) {
						param.put("role_id", role.getId());
						roleService.insertRoleRight(param);
					}
				}
			}
			else {
				Map<String,Object> param = new HashMap<String,Object>();
				param.put("id", id);
				param.put("name", name);
				param.put("rights", rights);
				roleService.updateRole(param);			
			}
			responseMap.put("msg", "ok");
			responseMap.put("success", "true");
			return responseMap;
		}catch(Exception e) {
			e.printStackTrace();
			responseMap.put("msg", "failure");
			responseMap.put("success", "false");
			responseMap.put("errors", e.getClass()+":"+e.getMessage());
			return responseMap;
		}
	}
}
